﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autocentro.Entities;
using System.Data.Objects;

namespace Autocentro.Respository
{
    public class DetalleVentaRepository:MasterRepository,IDetalleVentaRepository
    {
        public List<DETALLEVENTA> GetAllFromDetalleVentaByVentaId(int id)
        {
            var query = from c in context.DetalleVenta.Include("PRODUCTO")
                        where c.IdVenta.Equals(id)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.ToList();
        }


        public void DeleteDetalleVenta(int idDet , int idVenta)
        {
            var existe = context.DetalleVenta
                .Where(p => p.Item.Equals(idDet) && p.IdVenta.Equals(idVenta)).SingleOrDefault();
            if (existe!=null)
            {
                context.DetalleVenta.Remove(existe);
                context.SaveChanges();
            }
        }


        public DETALLEVENTA GetFromProductoByDetalleVentaId(int idProducto, int idVenta)
        {
            var query = from c in context.DetalleVenta
                        where c.IdProducto.Equals(idProducto) && c.IdVenta.Equals(idVenta)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.SingleOrDefault();
        }


        public List<DETALLEVENTA> GetFromDetalleVentaByProductoDescAndVentaId(string descripcion, int id)
        {
            if (descripcion != "")
            {
                var query = from c in context.DetalleVenta.Include("PRODUCTO")
                            where c.Producto.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            && c.IdVenta.Equals(id)
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query = from c in context.DetalleVenta.Include("PRODUCTO")
                            where c.IdVenta.Equals(id)
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
        }
    }
}
